package com.cangchu.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.*;
import android.widget.*;
import com.cangchu.R;
import com.cangchu.db.domain.DataRep;
import com.cangchu.db.domain.Goods;
import com.cangchu.ui.activity.GoodPagerActivity;

import java.util.List;


/**
 * 清单Fragment，从本地文件读取数据并且初始化ViewList。通过Menu可以对数据进行新增。
 * 点击List的Item ，可以进入详情页面。不同的权限可以进行不同的操作。
 * Created by 张博文 on 2016/9/29.
 */
public class CheckListFragment extends ListFragment {

    private static final String TAG = "CheckListFragment";
    private List<Goods> goodses;

    public static final int PAGE = 1;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
        getActivity().setTitle("清单列表");
        goodses = DataRep.getInstance(getActivity()).getGoodses();

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        if (view != null) {
            final ListView listView = (ListView) view.findViewById(android.R.id.list);
            listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);

            //多选批处理模式……
            listView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
                @Override
                public void onItemCheckedStateChanged(ActionMode actionMode, int i, long l, boolean b) {
                    View child = listView.getChildAt(i);
                    CheckBox checkBox = (CheckBox) child.findViewById(R.id.check_selected);
                    if (checkBox.isChecked()) {
                        checkBox.setChecked(false);
                    } else {
                        checkBox.setChecked(true);
                    }
                }

                @Override
                public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
                    MenuInflater inflater1 = actionMode.getMenuInflater();
                    inflater1.inflate(R.menu.ckeck_fragment_muliti, menu);
                    //修改adapter中view 的checkBox

                    for (int i = 0; i < listView.getChildCount(); i++) {
                        View child = listView.getChildAt(i);
                        CheckBox checkBox = (CheckBox) child.findViewById(R.id.check_selected);
                        checkBox.setVisibility(View.VISIBLE);
                    }
                    return true;
                }

                @Override
                public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
                    return false;
                }

                @Override
                public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
                    switch (menuItem.getItemId()) {
                        case R.id.menu_item_delete_crime:
                            CheckAdapter adapter = (CheckAdapter) getListAdapter();
                            //循环遍历adapter中管理的数据，如果发现item被选中，则删除对应的对象
                            for (int i = adapter.getCount() - 1; i >= 0; i--) {
                                if (getListView().isItemChecked(i)) {
                                    DataRep.getInstance(getActivity()).delete(adapter.getItem(i));
                                }
                            }
                            actionMode.finish();
                            adapter.notifyDataSetChanged();
                            return true;
                        default:
                            return false;
                    }
                }

                @Override
                public void onDestroyActionMode(ActionMode actionMode) {
                    for (int i = 0; i < listView.getChildCount(); i++) {
                        View child = listView.getChildAt(i);
                        CheckBox checkBox = (CheckBox) child.findViewById(R.id.check_selected);
                        checkBox.setVisibility(View.GONE);
                    }
                }
            });
        }

        CheckAdapter adapter = new CheckAdapter(goodses);
        setListAdapter(adapter);

        return view;
    }

    //Menu 按钮配置
    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.ckeck_fragment_list, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_item_new_goods:
                Log.d(TAG, "点击新增商品");
                openPageGood(new Goods());
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        Goods good = (Goods) (getListAdapter()).getItem(position);
        openPageGood(good);
    }

    private void openPageGood(Goods good) {
        Intent intent = new Intent(getActivity(), GoodPagerActivity.class);
        intent.putExtra(GoodPagerActivity.OBJECT, good);

        startActivityForResult(intent, PAGE);
    }
    // END of Menu 按钮配置

    @Override
    public void onPause() {
        super.onPause();
        DataRep.getInstance(getActivity()).serializableData();
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.d(TAG, "onActivityResult 被调用");
        if (requestCode == PAGE) {
            ((CheckAdapter) getListAdapter()).notifyDataSetChanged();
        }

    }

    //自定义的ListViewAdapter
    private class CheckAdapter extends ArrayAdapter<Goods> {

        public CheckAdapter(List<Goods> list) {
            super(getActivity(), android.R.layout.simple_list_item_1, list);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                getActivity().setTitle("新增物品");
                convertView = getActivity().getLayoutInflater().inflate(R.layout.list_item_check, null);
            }
            Goods goods = getItem(position);
            TextView codeTextView = (TextView) convertView.findViewById(R.id.check_list_item_codeTextView);
            TextView nameTextView = (TextView) convertView.findViewById(R.id.check_list_item_nameTextView);
            codeTextView.setText(goods.getCode());
            nameTextView.setText(goods.getName());
            return convertView;
        }
    }
}
